
// Definition for singly-linked list.
function ListNode(val, next) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
}

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
  if(!head) return null;
  if(!head.next) return head;
  let newHead = null;
  while(head) {
    let temp = newHead;
    newHead = head;
    head = head.next;
    newHead.next = temp;
  }
  return newHead;
};
let head = [1,2,3,4,5];
console.log(reverseList(head));
head = [1,2];
console.log(reverseList(head));
head = [];
console.log(reverseList(head));
